package cn.rongdao.jrkabao.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import com.mifengkong.frtools.util.FRDisplayMetrics;

import java.lang.ref.WeakReference;

import cn.rongdao.jrkabao.R;

/**
 * Created by jack on 2018/3/12
 */

public class IndicatorView extends View {

    private int indicatorColor = Color.rgb(0, 0, 0);
    private int indicatorColorSelected = Color.rgb(0, 0, 0);
    private int indicatorWidth = 0;
    private int indicatorHeight = 0;

    private int indicatorCount = 0;
    private int currentIndicator = 0;

    private NoLeakHandler handler;

    private Paint mPaint;

    public IndicatorView(Context context) {
        super(context);
    }

    public IndicatorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.IndicatorView);
            indicatorColor = typedArray.getColor(R.styleable.IndicatorView_indicatorColor, Color.rgb(0, 0, 0));
            indicatorColorSelected = typedArray.getColor(R.styleable.IndicatorView_indicatorColorSelected, Color.rgb(0, 0, 0));
            indicatorWidth = FRDisplayMetrics.dp2px(getContext(), typedArray.getInteger(R.styleable.IndicatorView_indicatorWidth, 0));
            indicatorHeight = FRDisplayMetrics.dp2px(getContext(), typedArray.getInteger(R.styleable.IndicatorView_indicatorHeight, 0));
            typedArray.recycle();
        }
        mPaint = new Paint();
        handler = new NoLeakHandler(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int totalWidth = indicatorWidth * (2 * indicatorCount - 1);

        mPaint.setAntiAlias(true);

        if (indicatorCount > 0) {
            for (int i = 0; i < indicatorCount; i++) {
                if (i == currentIndicator) {
                    mPaint.setColor(indicatorColorSelected);
                } else {
                    mPaint.setColor(indicatorColor);
                }
                @SuppressLint("DrawAllocation") RectF rectF = new RectF((getWidth() - totalWidth) / 2 + (i * 1.7f * indicatorWidth)
                        , (getHeight() - indicatorHeight) / 2
                        , ((getWidth() - totalWidth) / 2 + (i * 1.7f * indicatorWidth) + indicatorWidth)
                        , ((getHeight() - indicatorHeight) / 2 + indicatorHeight));
                canvas.drawRoundRect(rectF, FRDisplayMetrics.dp2px(getContext(), 2), FRDisplayMetrics.dp2px(getContext(), 2), mPaint);
            }
        }
    }

    public void setIndicatorCount(int indicatorCount) {
        if (indicatorCount > 1) {
            this.indicatorCount = indicatorCount;
        }
    }

    public void setCurrentIndicator(int currentIndicator) {
        if (indicatorCount > 1) {
            this.currentIndicator = currentIndicator;
            handler.sendEmptyMessage(0x12);
        }
    }

    private static class NoLeakHandler extends Handler {
        private WeakReference<IndicatorView> indicatorViewWeakReference;

        NoLeakHandler(IndicatorView indicatorView) {
            indicatorViewWeakReference = new WeakReference<>(indicatorView);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x12) {
                indicatorViewWeakReference.get().invalidate();
            }
        }
    }
}